package com.hx.vending.controller;

import com.hx.vending.annotation.Accredit;
import com.hx.vending.bean.Role;
import com.hx.vending.service.IRoleService;
import com.hx.vending.util.Msg;
import com.hx.vending.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/1/2.
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    /**
     * 获取角色列表
     * @param roleName 角色名称
     * @return
     */
    @RequestMapping(value = "/roleList")
    @ResponseBody
    public Object list(@RequestParam(required = false) String roleName) {
        Role role = new Role();
        if(ToolUtil.isNotEmpty(roleName)){
            role.setRoleName(roleName);
        }
        List<Role> roles = this.roleService.selectRoles(role);
        for(Role r:roles){
            String[] strs = roleService.selectMenuByRole(r.getRoleId());
            r.setMenuRoleList(strs);
        }

        return Msg.success().add("roleList",roles);
    }

    /**
     * 角色新增
     * @param role
     * @param result
     * @return
     */
    @RequestMapping(value = "/addRole",method = RequestMethod.POST)
    @ResponseBody
    @Accredit
    public Msg add(@RequestBody Role role, BindingResult result) {
        if (result.hasErrors()) {
            return Msg.fail().add("va_msg","请求有错误");
        }
        int i = this.roleService.insertRole(role);
        if(i > 0){
            return Msg.success();
        }
        return Msg.fail();
    }

    /**
     * 修改角色
     * @param role
     * @param result
     * @return
     */
    @RequestMapping(value = "/editRole")
    @ResponseBody
    @Accredit
    public Msg edit(@RequestBody Role role, BindingResult result) {
        if (result.hasErrors()) {
            return Msg.fail().add("msg","请求有错误");
        }
        int i = this.roleService.updateByPrimaryKey(role);
        if(i > 0){
            return Msg.success();
        }
        return Msg.fail();
    }
    /**
     * 删除角色
     */
    @RequestMapping(value = "/remove/{roleIds}",method = RequestMethod.DELETE)
    @ResponseBody
    @Accredit
    public Msg remove(@PathVariable("roleIds") String roleIds) {
        if (ToolUtil.isEmpty(roleIds)) {
            return Msg.fail().add("msg","角色id不能为空");
        }
        Msg msg = roleService.delRoleById(roleIds);
        if(msg.getCode() == 200){
            return Msg.success();
        }else{
            return Msg.fail();
        }
    }
    /**
     * 给角色设置权限
     * @param roleId 角色id
     * @param menuIds 菜单id
     * @return
     */
    @RequestMapping(value = "/setAuthority",method = RequestMethod.POST)
    @ResponseBody
    @Accredit
    public Msg setAuthority(@RequestParam("roleId") Integer roleId, @RequestParam("menuIds") String menuIds) {
        if (ToolUtil.isOneEmpty(roleId)) {
            return Msg.fail().add("msg","请求有错误");
        }
        Msg msg = this.roleService.setAuthority(roleId, menuIds);
        if(msg.getCode() == 200){
            return Msg.success();
        }else{
            return Msg.fail();
        }
    }

}
